from selenium import webdriver
from selenium.webdriver.support.select import Select
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from bs4 import BeautifulSoup
import time
import datetime
chrome_options = webdriver.ChromeOptions()
# chrome_options.add_argument('--headless')
# chrome_options.add_argument('--disable-gpu')
client = webdriver.Chrome(executable_path='D:\webdrivers\chromedriver.exe',
                          options=chrome_options)
#gitlab登录地址
url = "https://gitlab.gzspiral.com/"
userid = "/html/body/div[1]/div[2]/div/div[3]/div[2]/div/div/div/div/form/div[1]/input"
passwdid = "/html/body/div[1]/div[2]/div/div[3]/div[2]/div/div/div/div/form/div[2]/input"
username = "root"
passwd = "jlUmXna1^DW%*F#K"
loginbutton = "/html/body/div[1]/div[2]/div/div[3]/div[2]/div/div/div/div/form/div[5]/input"

class gitlabmanager(object):
    def __init__(self, name="", email="", newuser="", giturl="", pri="develop"):
    #员工姓名
        self.name = name
    #员工的email地址
        self.email = email
    #员工的英文拼音
        self.newuser = newuser
    #员工需要加权限的git地址
        self.giturl = giturl
    #员工的git权限
        self.pri = pri
    def Login(self):
        client.get(url)
    #账号登录-输入账号密码
        elemid = client.find_element_by_xpath(userid)
        elempasswd = client.find_element_by_xpath(passwdid)
        time.sleep(1)
        elemid.send_keys(username)
        elempasswd.send_keys(passwd)
    #点击登录
        elemlogin = client.find_element_by_xpath(loginbutton)
        elemlogin.click()

    def Adduser(self):
    #进入管理中心
        client.get(url+"admin")
    #新建用户
        client.find_element_by_xpath('//*[@id="content-body"]/div[2]/div[2]/div[2]/div/div[1]/a').click()
    #添加名称
        client.find_element_by_xpath("/html/body/div[3]/div/div[3]/main/div[2]/form/fieldset[1]/div[1]/div[2]/input").send_keys(self.name)
    #添加Username
        client.find_element_by_xpath("/html/body/div[3]/div/div[3]/main/div[2]/form/fieldset[1]/div[2]/div[2]/input").send_keys(self.newuser)
    #添加email
        client.find_element_by_xpath("/html/body/div[3]/div/div[3]/main/div[2]/form/fieldset[1]/div[3]/div[2]/input").send_keys(self.email)
    #点击创建按钮
        client.find_element_by_xpath("/html/body/div[3]/div/div[3]/main/div[2]/form/div/input").click()
    #编辑
        client.find_element_by_xpath("/html/body/div[3]/div/div[3]/main/h3/div/a[2]").click()
    #输入账号密码
        client.find_element_by_xpath("/html/body/div[3]/div/div[3]/main/div[2]/form/fieldset[2]/div[1]/div[2]/input").send_keys("12345678")
        client.find_element_by_xpath("/html/body/div[3]/div/div[3]/main/div[2]/form/fieldset[2]/div[2]/div[2]/input").send_keys("12345678")
        client.find_element_by_xpath("/html/body/div[3]/div/div[3]/main/div[2]/form/div/input").click()
    def Dongjieuser(self):
        ##禁用用户
        client.get(url + "admin/" + "users?utf8=%E2%9C%93&search_query=" + self.email)
        client.find_element_by_xpath("/html/body/div[3]/div/div[3]/main/div[2]/div[1]/div[3]/div[2]/div[5]/div/button").click()
        client.find_element_by_xpath("/html/body/div[3]/div/div[3]/main/div[2]/div[1]/div[3]/div[2]/div[5]/div/ul/li[2]/button").click()
        time.sleep(1)
        client.find_element_by_xpath("/html/body/div[6]/div[1]/div/div/footer/button[2]").click()
    def addpri(self):
        #合成git成员列表地址
        a = "groups/"
        b = "/-/group_members"
        str_list = list(self.giturl)
        str_list.insert(28, a)
        git_list = ''.join(str_list)
        client.get(git_list+b)
        #填写员工账号
        client.find_element_by_xpath(
            "/html/body/div[3]/div/div[3]/main/div[2]/div/div[2]/div[1]/div/div/form/div[1]/div/ul/li/input").send_keys(
            self.newuser)
        time.sleep(1)
        client.find_element_by_xpath("/html/body/div[21]/ul/li/div/div/div[2]/div[1]").click()
        #下拉框选择角色权限
        userpri = "/html/body/div[3]/div/div[3]/main/div[2]/div/div[2]/div[1]/div/div/form/div[2]/div[1]/select"
        if self.pri == "maintainer":
            Select(client.find_element_by_xpath(userpri)).select_by_value("40")
        else:
            Select(client.find_element_by_xpath(userpri)).select_by_value("30")
        #点击邀请
        client.find_element_by_xpath(
            "/html/body/div[3]/div/div[3]/main/div[2]/div/div[2]/div[1]/div/div/form/input[3]").click()


